package njust.dao.impl;

import java.util.Date;
import java.util.List;

import njust.dao.UserDao;
import njust.domain.User;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
	 
	
	public String addUser(User user){
        if(this.getHibernateTemplate().find("from User u where u.username=?",user.getUsername()).size()==0)
		{this.getHibernateTemplate().save(user);
		 
		return "ok";}
        else return "no";
		}
	public boolean updateUser(User user){
		this.getHibernateTemplate().update(user);
		 return true;	
	}
	public boolean deleteUser(User user){
		
		this.getHibernateTemplate().delete(user);
		 return true;
	}
	

	


/////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	
	public List getlistUser(){
		List list=this.getHibernateTemplate().find("from User order by UserID desc");
		return list;
		
	}
	public User getUserById(int id){
		User user=(User)this.getHibernateTemplate().get(User.class, id);
		return  user;
		
	}
	public User getUserByName(String name){
		if(this.getHibernateTemplate().find("from User  where UserName='"+name+"'").size()==0)
			return null;
		else
		{List list=this.getHibernateTemplate().find("from User  where UserName='"+name+"'");
		User user=(User) list.get(0);
		return  user;}
	}
	
    
	
	
}
